package com.demos.springcloud.gateway05_custLocalFilter.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 自定义GateWay过滤器
 */
public class GetGateWayFilter implements GatewayFilter, Ordered {

    private GetGateWayFilterParam param;

    public GetGateWayFilter(GetGateWayFilterParam param) {
        this.param = param;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println(Thread.currentThread().getName() + " getGateway_pre：" + param.getParaName() + System.currentTimeMillis()); //前置方法
        return chain.filter(exchange)
                .then(Mono.fromRunnable(() -> System.out.println(Thread.currentThread().getName() + " getGateway_aft：" + param.getParaName() + System.currentTimeMillis())));
    }

    @Override
    public int getOrder() {
        return 10; //优先级，越大优先级越低（不能定义相同优先级；此为全局优先级部分过滤器种类）
    }
}
